import * as echarts from 'echarts';

type EChartsOption = echarts.EChartsOption;

var option: EChartsOption;
var myChart: any;

export const initPointPie = (dataInfo, dataTotalCount, seriesName) => {
  // myChart ? myChart.clear() : ''
  if (myChart != null && myChart != '' && myChart != undefined) {
    myChart.dispose()
  }
  var chartDom = document.getElementById('pie-data-count-point');
  method(chartDom, dataInfo, dataTotalCount, seriesName)
}

export const initLinePie = (dataInfo, dataTotalCount, seriesName) => {
  if (myChart != null && myChart != '' && myChart != undefined) {
    myChart.dispose()
  }
  var chartDom = document.getElementById('pie-data-count-line');
  method(chartDom, dataInfo, dataTotalCount, seriesName)
}

export const initSurfacePie = (dataInfo, dataTotalCount, seriesName) => {
  if (myChart != null && myChart != '' && myChart != undefined) {
    myChart.dispose()
  }
  var chartDom = document.getElementById('pie-data-count-surface');
  method(chartDom, dataInfo, dataTotalCount, seriesName)
}

const method = (chartDom,dataInfo, dataTotalCount, seriesName) => {
  myChart = echarts.init(chartDom);
  option = getPie(dataInfo, dataTotalCount, seriesName)
  option && myChart.setOption(option);
}

const getPie = (dataInfo, dataTotalCount, seriesName) => {
  let pieData = []
  for (let i = 0; i < dataInfo.length; i++) {
    pieData.push({
      name: dataInfo[i].category,
      value: dataInfo[i].count
    })
  }

  let options = {
    title: {
      text: '统计',
      subtext: '总数：' + dataTotalCount,
      left: 'center'
    },
    tooltip: {
      trigger: 'item'
    },
    // legend: {
    //   orient: 'horizontal',  //显示方式
    //   left: 'left',  //对齐方式
    //   top: 'bottom', //位置
    // },
    color: ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'],
    series: [
      {
        name: seriesName,
        type: 'pie',
        radius: '50%',
        data: pieData,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }

  return options
}
